<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="职能部门" prop="functionalDeptName">
        <el-input
          v-model="queryParams.functionalDeptName"
          placeholder="请输入职能部门名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="职能类型" prop="functionalDeptType">
        <el-select v-model="queryParams.functionalDeptType" placeholder="职能类型" @change="getList" clearable>
          <el-option
            v-for="dict in dict.type.fun_dept_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="数据状态" prop="dataStatus">
        <el-select v-model="queryParams.dataStatus" placeholder="数据状态" @change="getList" clearable>
          <el-option
            v-for="dict in dict.type.data_status_examine_v3"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="地址" prop="address" v-show="isFoldQuery">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="创建人" prop="createBy" v-show="isFoldQuery">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" v-show="isFoldQuery">
        <el-date-picker
          v-model="createDateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="修改人" prop="updateBy" v-show="isFoldQuery">
        <el-input
          v-model="queryParams.updateBy"
          placeholder="请输入修改人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="修改时间" v-show="isFoldQuery">
        <el-date-picker
          v-model="updateDateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button
          plain
          :icon="isFoldQuery ? 'el-icon-caret-top' : 'el-icon-caret-bottom'"
          size="mini"
          @click="foldQuery"
        >
          {{ isFoldQuery ? '折叠' : '展开' }}
        </el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['dataBase:functDept:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['dataBase:functDept:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch"  @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="functDeptList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="主键id" align="center" prop="id" v-if="false"/>
      <el-table-column label="职能部门" align="center" prop="functionalDeptName" fixed width="200"/>
      <el-table-column label="类型" align="center" prop="functionalDeptType" fixed width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.fun_dept_type" :value="scope.row.functionalDeptType"/>
        </template>
      </el-table-column>
      <el-table-column label="联系方式" align="center" prop="phone" width="160"/>
      <el-table-column label="地址" align="center" prop="address" width="400"/>
      <el-table-column label="经度" align="center" prop="longitude" width="120"/>
      <el-table-column label="纬度" align="center" prop="latitude" width="120"/>
      <el-table-column label="备注" align="center" prop="remark" width="220"/>
      <el-table-column label="创建人" align="center" prop="createBy" width="220"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="220"/>
      <el-table-column label="最后修改人" align="center" prop="updateBy" width="220"/>
      <el-table-column label="修改时间" align="center" prop="updateTime" width="220"/>
      <el-table-column label="数据状态" align="center" prop="dataStatus" width="120" fixed="right">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.data_status_examine_v3" :value="scope.row.dataStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit-outline"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['dataBase:functDept:edit']"
            v-show="scope.row.dataStatus ===0 || scope.row.dataStatus ===2 ||scope.row.dataStatus ===4"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document-checked"
            @click="handleUp(scope.row)"
            v-hasPermi="['dataBase:functDept:edit']"
            v-show="scope.row.dataStatus ===0 || scope.row.dataStatus ===2"
          >提交
          </el-button>

          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-close"-->
          <!--            @click="handleUpdate(scope.row)"-->
          <!--            v-hasPermi="['dataBase:functDept:edit']"-->
          <!--            v-show="scope.row.dataStatus ===1"-->
          <!--          >撤回</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-remove-outline"
            @click="handleStop(scope.row)"
            v-hasPermi="['dataBase:functDept:edit']"
            v-show="scope.row.dataStatus ===3"
          >停用
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-check"
            @click="handleStart(scope.row)"
            v-hasPermi="['dataBase:functDept:edit']"
            v-show="scope.row.dataStatus ===4"
          >启用
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['dataBase:functDept:remove']"
            v-show="scope.row.dataStatus ===0 || scope.row.dataStatus ===4"
          >删除
          </el-button>
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-check"-->
          <!--            @click="handleReviewCheck(scope.row)"-->
          <!--            v-hasPermi="['dataBase:functDept:review']"-->
          <!--            v-show="scope.row.dataStatus ===1"-->
          <!--          >审核通过-->
          <!--          </el-button>-->
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-close"-->
          <!--            @click="handleReviewClose(scope.row)"-->
          <!--            v-hasPermi="['dataBase:functDept:review']"-->
          <!--            v-show="scope.row.dataStatus ===1"-->
          <!--          >审核驳回-->
          <!--          </el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改职能部门对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" :disabled="form.dataStatus === 1">
        <el-form-item label="部门名称" prop="functionalDeptName">
          <el-input v-model="form.functionalDeptName" placeholder="请输入职能部门名称"/>
        </el-form-item>
        <el-form-item label="部门类型" prop="functionalDeptType">
          <el-select v-model="form.functionalDeptType" placeholder="请选择职能部门类型" style="width:100%">
            <el-option
              v-for="dict in dict.type.fun_dept_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="联系方式" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系方式"/>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" readonly @click.native="openLocationPicker"
                    @focus="openLocationPicker" style="width: 80%;"
          />
          <el-button type="primary" size="mini" @click="openLocationPicker" style="margin-left: 6px;">
            地图选点
          </el-button>
        </el-form-item>
        <el-form-item label="经纬度" prop="longitude">
          <el-row :gutter="10">
            <el-col :span="8">
              <el-input v-model="form.longitude" placeholder="请选择地址" disabled />
            </el-col>
            <el-col :span="1">
              ，
            </el-col>
            <el-col :span="8" >
              <el-input  v-model="form.latitude" placeholder="请选择地址" disabled />
            </el-col>
          </el-row>
        </el-form-item>
<!--        <el-form-item label="经度" prop="longitude" v-show="false">-->
<!--          <el-input v-model="form.longitude" placeholder="请输入经度" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="纬度" prop="latitude" v-show="false">-->
<!--          <el-input v-model="form.latitude" placeholder="请输入纬度" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="备注" prop="remark">-->
<!--          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm2" v-show="form.dataStatus !== 1">提 交
        </el-button>
        <el-button :loading="buttonLoading" type="primary" @click="submitForm1" v-show="form.dataStatus !== 1">保 存
        </el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <location-picker
      :visible.sync="locationDialogVisible"
      :value="pickedLocation"
      @input="onLocationPicked"
    />
  </div>
</template>

<script>
import { listFunctDept, getFunctDept, delFunctDept, addFunctDept, updateFunctDept } from '@/api/dataBase/functDept'
import LocationPicker from '@/components/Map/MapSelector.vue'

export default {
  name: 'FunctDept',
  dicts: ['fun_dept_type', 'data_status_examine_v3'],
  components: { LocationPicker },
  data() {
    return {
      isFoldQuery:false,
      // 日期范围
      createDateRange: [],
      updateDateRange: [],
      locationDialogVisible: false,
      pickedLocation: {
        lng: null,
        lat: null,
        address: ''
      },
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 职能部门表格数据
      functDeptList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        functionalDeptName: undefined,
        functionalDeptType: undefined,
        phone: undefined,
        address: undefined,
        longitude: undefined,
        latitude: undefined,
        createBy: undefined,
        updateBy: undefined,
        dataStatus: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: '主键id不能为空', trigger: 'blur' }
        ],
        functionalDeptName: [
          { required: true, message: '职能部门名称不能为空', trigger: 'blur' }
        ],
        functionalDeptType: [
          { required: true, message: '职能部门类型不能为空', trigger: 'change' }
        ],
        phone: [
          {
            required: true,
            pattern: /(^1[3-9]\d{9}$)|(^0\d{2,3}-?\d{7,8}$)/,
            message: '请输入正确的手机号码或座机号（座机示例：027-12345678）',
            trigger: 'blur'
          }
        ],
        address: [
          { required: true, message: '地址不能为空', trigger: 'change' }
        ],
        longitude: [
          { required: true, message: '经度不能为空', trigger: 'blur' }
        ],
        latitude: [
          { required: true, message: '纬度不能为空', trigger: 'blur' }
        ],
        dataStatus: [
          { required: true, message: '数据状态不能为空', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询职能部门列表 */
    getList() {
      this.loading = true
      this.queryParams.params = {}
      if (null != this.createDateRange && '' != this.createDateRange) {
        this.queryParams.params['beginCreateTime'] = this.createDateRange[0]
        this.queryParams.params['endCreateTime'] = this.createDateRange[1]
      }
      if (null != this.updateDateRange && '' != this.updateDateRange) {
        this.queryParams.params['beginUpdateTime'] = this.updateDateRange[0]
        this.queryParams.params['endUpdateTime'] = this.updateDateRange[1]
      }
      listFunctDept(this.queryParams).then(response => {
        this.functDeptList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    openLocationPicker() {
      this.pickedLocation = {
        lng: this.form.longitude,
        lat: this.form.latitude,
        address: this.form.address
      }
      this.locationDialogVisible = true
    },

    onLocationPicked(loc) {
      // loc: { lng, lat, address }
      this.form.longitude = loc.lng
      this.form.latitude = loc.lat
      this.form.address = loc.address
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        functionalDeptName: undefined,
        functionalDeptType: undefined,
        phone: undefined,
        address: undefined,
        longitude: undefined,
        latitude: undefined,
        delFlag: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        remark: undefined,
        dataStatus: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      console.log("点击重置按钮")
      this.createDateRange = []
      this.updateDateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    foldQuery() {
      this.isFoldQuery = !this.isFoldQuery;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加职能部门'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true
      this.reset()
      const id = row.id || this.ids
      getFunctDept(id).then(response => {
        this.loading = false
        this.form = response.data
        this.open = true
        this.title = '修改职能部门'
      })
    },
    /** 提交按钮 */
    submitForm1() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.buttonLoading = true
          this.form.dataStatus = 0
          if (this.form.id != null) {
            updateFunctDept(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            }).finally(() => {
              this.buttonLoading = false
            })
          } else {
            addFunctDept(this.form).then(response => {
              this.$modal.msgSuccess('保存成功')
              this.open = false
              this.getList()
            }).finally(() => {
              this.buttonLoading = false
            })
          }
        }
      })
    },
    submitForm2() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.buttonLoading = true
          this.form.dataStatus = 1
          if (this.form.id != null) {
            updateFunctDept(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            }).catch((error) => {
              console.log('111')
              this.form.dataStatus = 0
            }).finally(() => {
              this.buttonLoading = false
            })
          } else {
            addFunctDept(this.form).then(response => {
              this.$modal.msgSuccess('提交成功')
              this.open = false
              this.getList()
            }).catch((error) => {
              console.log('111')
              this.form.dataStatus = 0
            }).finally(() => {
              this.buttonLoading = false
            })
          }
        }
      })
    },
    handleUp(row) {
      this.$modal.confirm('是否确认提交？').then(() => {
        this.loading = true
        row.dataStatus = 3
        return updateFunctDept(row)
      }).then(() => {
        this.loading = false
        this.getList()
        this.$modal.msgSuccess('提交成功')
      }).catch(() => {
        this.getList()
      }).finally(() => {
        this.loading = false
      })
    },
    handleStart(row) {
      this.$modal.confirm('是否确认启用？').then(() => {
        this.loading = true
        row.dataStatus = 3
        return updateFunctDept(row)
      }).then(() => {
        this.loading = false
        this.getList()
        this.$modal.msgSuccess('启用成功')
      }).catch(() => {
      }).finally(() => {
        this.loading = false
      })
    },
    handleStop(row) {
      this.$modal.confirm('是否确认停用？').then(() => {
        this.loading = true
        row.dataStatus = 4
        return updateFunctDept(row)
      }).then(() => {
        this.loading = false
        this.getList()
        this.$modal.msgSuccess('停用成功')
      }).catch(() => {
      }).finally(() => {
        this.loading = false
      })
    },
    handleReviewCheck(row) {
      this.$modal.confirm('是否确认通过审核？').then(() => {
        this.loading = true
        row.dataStatus = 3
        return updateFunctDept(row)
      }).then(() => {
        this.loading = false
        this.getList()
        this.$modal.msgSuccess('审核通过成功')
      }).catch(() => {
        this.getList()
      }).finally(() => {
        this.loading = false
      })
    },
    handleReviewClose(row) {
      this.$modal.confirm('是否确认驳回？').then(() => {
        this.loading = true
        row.dataStatus = 2
        return updateFunctDept(row)
      }).then(() => {
        this.loading = false
        this.getList()
        this.$modal.msgSuccess('驳回成功')
      }).catch(() => {
        this.getList()
      }).finally(() => {
        this.loading = false
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除职能部门编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true
        return delFunctDept(ids)
      }).then(() => {
        this.loading = false
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      }).finally(() => {
        this.loading = false
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('dataBase/functDept/export', {
        ...this.queryParams
      }, `functDept_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
